﻿/******************************************************************************
 * Copyright 2020-xxxx xxx Co., Ltd.
 * All right reserved. See COPYRIGHT for detailed Information.
 *
 * @projectName   aaa
 * @file          xpushbuttonfunction.h
 * @brief         XXXX Function
 *
 * @author        Feng<2859352492@qq.com>
 * @date          2022/01/17 13:57:15
 * @history
 *****************************************************************************/

#ifndef XPushButtonFunction_H
#define XPushButtonFunction_H

#include <QWidget>
#include <QPushButton>
#include <QPainter>
#include <QMouseEvent>
#include <QApplication>
//#include <QColorDialog>

#include <QMap>
#include <QThread>
#include <QMutex>
#include <QDebug>

#include "xnamespace.h"
#include "xfunction.h"

QT_BEGIN_NAMESPACE

typedef struct XMap{
    QMap<int, XFunction> xFunctionMap;
    QMap<int, Function> functionMap;
    int length = 0;
}XMap;

class XPushButtonFunctionWorker;
class XPushButtonFunctionPrivate;
class XPushButtonFunction : public QPushButton
{
    Q_OBJECT

    Q_PROPERTY(bool displayEnable READ getDisplayEnable WRITE setDisplayEnable)

public:
    explicit XPushButtonFunction (QWidget *parent = nullptr);
    ~XPushButtonFunction ();

    /* Property Interface */
    bool getDisplayEnable() const;
    void setDisplayEnable(bool);

    /* Other Interface */
    bool gettTraversalInstanceStatus() const;
    bool getPushButtonBoolStatus() const;

    void insertFunction(const PushButtonStatus status, XFunction &xFunction, Function &function);
    bool deleteFunction(const PushButtonStatus status, const int index);
    void moveUpFunction(const PushButtonStatus status, const int index);
    void moveDownFunction(const PushButtonStatus status, const int index);

    void traversalPressInstanceMap();
    void traversalPressStatusMap();
    void traversalReleaseInstanceMap();
    void traversalReleaseStatusMap();

protected:
    void paintEvent(QPaintEvent *ev) override;
    void enterEvent(QEvent *ev) override;
    void leaveEvent(QEvent *ev) override;
    void mousePressEvent(QMouseEvent *ev) override;
    void mouseReleaseEvent(QMouseEvent *ev) override;
    void mouseMoveEvent(QMouseEvent *ev) override;

private:
    Q_DISABLE_COPY(XPushButtonFunction)
    XPushButtonFunctionPrivate *xPushButtonFunctionP = nullptr;

    QMutex mutex;
    XPushButtonFunctionWorker *worker;
    QThread *workerThread;
    bool traversalInstanceStatus;
};

class XPushButtonFunctionWorker : public QObject
{
    Q_OBJECT
public:
    XPushButtonFunctionWorker(XPushButtonFunction *parent);

public slots:
    void doWork();
    void stop();

private:
    XPushButtonFunction *pushButtonFunction;
    QMutex mutex;
    bool isStop;
};

QT_END_NAMESPACE

#endif // ZHUANDAXI_H
